<?php
/** [模型表数据模型]
 * @Author: 976123967@qq.com
 * @Date:   2015-01-09 16:51:52
 * @Last Modified by:   chenli
 * @Last Modified time: 2015-03-05 23:41:48
 */
class ModelModel extends Model{

	// 表名称
	protected $tableName = 'model';



	/**
	 * [alter_edit_table 更新表结构]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function alter_edit_table($data)
	{
		// 更改表名称
		// sql语句
		$sql = 'alter table '.C('DB_PREFIX').'article_'.$data['oldename']."  rename ".C('DB_PREFIX').'article_'.$data['name'];
		$this->query($sql);
		// 更改表注释
		$sql = 'alter table '.C('DB_PREFIX').'article_'.$data['name'] .' COMMENT="'.$data['remark'].'"';
		$this->query($sql);
	}

	/**
	 * [alter_drop_table 删除表]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function alter_drop_table($data)
	{
		$sql = 'drop table '.C('DB_PREFIX').'article_'.$data['name'];
		$this->query($sql);
	}

	
	/**
	 * [alter_add_table 添加表]
	 * @param [type] $data [description]
	 */
	public function alter_add_table($data)
	{

		// 创建表
		$sql = file_get_contents(BASE_LIB_PATH."Data/table.sql");
		$sql = preg_replace(array('/thinkcms_article_data/','/文章附表/'), array(C('DB_PREFIX')."article_".$data['name'],$data['remark']), $sql);
		$this->query($sql);




		// 添加字段
		$data = array(
			array(
				'model_mid'=>$data['mid'],
				'fname'=>'article_aid',
				'title'=>'关联字段',
				'validate'=>'',
				'require'=>0,
				'show_type'=>1,
				'show_lists'=>0,
				'is_system'=>1,
				'is_disabled'=>0,
				'sort'=>1,
			),
			array(
				'model_mid'=>$data['mid'],
				'fname'=>'body',
				'title'=>'详细内容',
				'validate'=>'',
				'require'=>0,
				'show_type'=>3,
				'show_lists'=>0,
				'is_system'=>0,
				'is_disabled'=>0,
				'sort'=>900,
			)

		);
		$modelFieldModel = D('ModelField');
		foreach($data as $v)
		{
			$modelFieldModel->add($v);
		}
	}

	
	
}